import { Scene_Base } from "@/scenes/Scene_Base.js";
import { Scene_Map } from "@/scenes/Scene_Map.js";

import { config } from "@/managers/ConfigManager.js";
import { SceneManager } from "@/managers/SceneManager.js";
import { ImageManager } from "@/managers/ImageManager.js";
// import { AudioManager } from "@/managers/AudioManager.js";
// import { DataManager } from "@/managers/DataManager.js";
import { Sprite } from "@/sprites/Sprite.js";
import { Sprite_Canvas } from "@/sprites/Sprite_Canvas.js";

import { Window_TitleCommand } from "@/windows/Window_TitleCommand.js";

export class Scene_Title extends Scene_Base {
    constructor() {
        super();
        this._backSprite1=null;
        this._backSprite2=null;
        this._gameTitleSprite =null;
    }

    create() {
        super.create();
        this.createBackground();
        this.createForeground();
        this.createCommandWindow();
    }

    start() {
        super.start();
        SceneManager.clearStack();
        this.centerSprite(this._backSprite1);
        this.centerSprite(this._backSprite2);
        
        //this.playTitleMusic();
        this.startFadeIn(this.fadeSpeed());
    }

    createBackground() {
        this._backSprite1=new Sprite();
        this._backSprite2=new Sprite();
        this.addChild(this._backSprite1);
        this.addChild(this._backSprite2);
        ImageManager.loadTitle1($dataSystem.title1Name).then(source=>{
            this._backSprite1.source=source;
        });
        ImageManager.loadTitle2($dataSystem.title2Name).then(source=>{
            this._backSprite2.source=source;
        });
        
    }

    createForeground() {
        if ($dataSystem.optDrawTitle) {
            this.drawGameTitle();
        }
    }

    drawGameTitle() {
        let x = 20;
        let y = config.height / 4;
        let maxWidth = config.width - x * 2;
        let text = $dataSystem.gameTitle;
        if(!this._gameTitleSprite){
            this._gameTitleSprite = new Sprite_Canvas(maxWidth,96);
            this.addChild(this._gameTitleSprite);
        }

        this._gameTitleSprite.x=x;
        this._gameTitleSprite.y=y;

        this._gameTitleSprite.outlineColor = '#000';
        this._gameTitleSprite.outlineWidth = 8;
        this._gameTitleSprite.fontSize = 72;
        this._gameTitleSprite.drawText(text, maxWidth/2, 0, maxWidth, 96,'center');
    }

    /**
     * 
     * @param {Sprite} sprite 
     */
    centerSprite(sprite) {
        sprite.x = config.width / 2;
        sprite.y = config.height / 2;
        sprite.anchor.set(0.5);
    }

    createCommandWindow() {
        this._commandWindow = new Window_TitleCommand(240,config.height);
        this._commandWindow.x=config.width/2-120;
        this._commandWindow.y=config.height-this._commandWindow.height-96;
        this.addChild(this._commandWindow);
    }

    playTitleMusic() {
        // AudioManager.playBgm($dataSystem.titleBgm);
        // AudioManager.stopBgs();
        // AudioManager.stopMe();
    }

    isBusy() {
        return this._commandWindow.isClosing()||super.isBusy();
    }

    stop(){
        super.stop();
        this.fadeOutAll();
    }
}